﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SilverQuery;

namespace SilverQueryDemo.jqia
{
	public class BootCloset : SilverQueryComponent
	{
		private jQuery _styleDropDown;
		private jQuery _colorDropDown;
		private jQuery _sizeDropDown;

		public override void ready()
		{
			_colorDropDown = @q("#colorDropdown");
			_sizeDropDown = @q("#sizeDropdown");

			_styleDropDown = @q("#styleDropdown").change((e) =>
				{
					var styleValue = @q(e.target).val();
					if (String.IsNullOrEmpty(styleValue))
						ShowEmptyDetails();
					else
						Ajax.get("BootClosetService.svc/boot/" + styleValue, null, OnGetBootCompleted);
									
					return false;
				});

			LoadDropDowns();
		}

		private void LoadDropDowns()
		{
			Ajax.get("BootClosetService.svc/boots", null, (json, error) =>
				{
					// Use NewtonSoft.Json.Linq library to parse the json response.
					var options = new List<SelectOption>(JArray.Parse(json).map((index, token) => 
					{
						return new SelectOption() { 
							Value = token["Key"].Value<string>(), 
							Caption = token["Value"].Value<string>() 
						};
					}));

					options.Insert(0, new SelectOption() { Caption = "Select a boot style", Value = string.Empty });

					_styleDropDown.loadSelect(options);
				});

			Ajax.get("BootClosetService.svc/sizes", null, (json, error) =>
				{
					// Use NewtonSoft.Json.Linq library to parse the json response.
					var options = new List<SelectOption>(JArray.Parse(json).map((index, token) =>
					{
						return new SelectOption()
						{
							Value = string.Empty,
							Caption = token.Value<string>()
						};
					}));

					_sizeDropDown.loadSelect(options).attr("disabled", "disabled");
				});
		}

		private void ShowEmptyDetails()
		{
			// Empty the color drop down and make it disabled.
			_colorDropDown.emptySelect().attr("disabled", "disabled");
			_sizeDropDown.attr("disabled", "disabled");
			@q("#detailsDisplay span").html("---");
			@q("#detailsPhoto").attr("src", "photos/none.png");
		}

		private void OnGetBootCompleted(string json, Exception error)
		{
			if (error != null || String.IsNullOrEmpty(json))
			{
				ShowEmptyDetails();
				return;
			}

			BootStyle boot = JsonConvert.DeserializeObject<BootStyle>(json);

			// Turn the list of colors into a collection of SelectOption
			var options = boot.Colors.map<string,SelectOption>((index, color)=> 
					{
						return new SelectOption() { Caption = color, Value = string.Empty };
					});

			_colorDropDown.removeAttr("disabled").loadSelect(options);
			_sizeDropDown.removeAttr("disabled");

			@q("#detailsName").html(boot.Name);
			@q("#detailsSku").html(boot.Sku.ToString());
			@q("#detailsColors").html(String.Join(", ", boot.Colors));
			@q("#detailsLining").html(boot.Lining);
			@q("#detailsPrice").html(boot.Price.ToString("C"));
			@q("#detailsHeight").html(boot.Height);
			@q("#detailsFeatures").html(boot.Features);
			@q("#detailsPhoto").attr("src", String.Format("photos/{0}.png", boot.Sku));

			// Wire up the termifier plugin for the new boot details.
			@q("#detailsDisplay abbr").termifier(new map() { { "lookupResource", "BootClosetService.svc/term/{0}" } });
		}
	}

	[DataContract]
	public class BootStyle
	{
		[DataMember]
		public int Sku { get; set; }
		[DataMember]
		public string Name { get; set; }
		[DataMember]
		public string Height { get; set; }
		[DataMember]
		public string Lining { get; set; }
		[DataMember]
		public string[] Colors { get; set; }
		[DataMember]
		public double Price { get; set; }
		[DataMember]
		public string Features { get; set; }
	}

	[DataContract]
	public class SelectOption
	{
		[DataMember]
		public string Value { get; set; }
		[DataMember]
		public string Caption { get; set; }
	}
}
